import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Juntao.Zhang on 2014/8/27.
 */
public class IpTest {
  public static void main(String[] args) {
//    try {
//      System.out.println("192.168.1.1".replaceAll(".", "/"));
//      InetAddress addr = InetAddress.getLocalHost();
//      new ArrayList<String>().toArray(new String[3]);
//      System.out.println(addr.getHostAddress());
////      System.out.println(InetAddress.getByName("10.4.25.130"));
//    } catch (UnknownHostException e) {
//      e.printStackTrace();
//    }
    int[] pre={1,2,3,4,5,6,7};
    int[] in={3,2,4,1,6,5,7};
    new Solution().reConstructBinaryTree(pre,in);
  }


  static class Solution {
    static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;

      TreeNode(int x) {
        val = x;
      }
    }

    public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
      Integer[] t=new Integer[in.length];
      for(int i=0;i<in.length;i++){
        t[i]=in[i];
      }
      return t1(0, pre, t);
    }


    public TreeNode t1(int i, int[] pre, Integer[] t) {
      List<Integer> tmp1 = new ArrayList<Integer>();
      List<Integer> tmp2 = new ArrayList<Integer>();
      boolean flag = true;
      for (int j = 0; j < t.length; j++) {
        if (pre[i] == t[j]) {
          flag = false;
          continue;
        }
        if (flag) {
          tmp1.add(t[j]);
        } else {
          tmp2.add(t[j]);
        }

      }
      TreeNode node = new TreeNode(pre[i]);
      if (tmp1.size() > 0) {
        TreeNode left = t1(i + 1, pre, tmp1.toArray(new Integer[tmp1.size()]));
        node.left = left;
      }

      if (tmp2.size() > 0) {
        TreeNode right = t1(i + tmp1.size() + 1, pre, tmp2.toArray(new Integer[tmp2.size()]));
        node.right = right;
      }
      return node;
    }
  }

}



